package com.ruoyi.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.dto.CanshuDTO;
import com.ruoyi.project.domain.dto.DayangDTO;
import com.ruoyi.project.domain.entity.Canshu;
import com.ruoyi.project.domain.entity.Dayang;
import com.ruoyi.project.service.CanshuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * (Canshu)表控制层
 *
 * @author makejava
 * @since 2025-06-29 16:17:34
 */
@RestController
@RequestMapping("/canshu")
public class CanshuController {

    @Autowired
    private CanshuService service;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param param 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    public Page<?> listPage(Page<Canshu> page, CanshuDTO param) {
        return this.service.listPage(page, param);
    }

    @GetMapping("/dict")
    public R<?> getDict() {
        return R.ok(this.service.getDictData());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/id/{id}")
    public R<?> getInfo(@PathVariable Long id) {
        return R.ok(this.service.getById(id));
    }

    /**
     * 新增/修改数据
     *
     * @param param 实体参数
     * @return 修改结果
     */
    @SaCheckPermission("canshu:index:save")
    @PutMapping("/save")
    public R<?> saveOrUpdate(@RequestBody CanshuDTO param) {
        return R.ok(this.service.saveOrUpdate(param) ? "保存成功" : "保存失败");
    }

    @SaCheckPermission("canshu:index:save")
    @PutMapping("/disabled")
    public R<?> disabled(@RequestBody Integer id) {
        return R.ok(this.service.disabled(id) ? "禁用成功" : "禁用失败");
    }

    /**
     * 删除数据
     *
     * @param ids 主键集合
     * @return 删除结果
     */
    @SaCheckPermission("canshu:index:remove")
    @DeleteMapping("/remove")
    public R delete(String[] ids) {
        return R.ok(this.service.removeByIds(CollUtil.toList(ids)) ? "删除成功" : "删除失败");
    }

    /**
     * 导入数据
     *
     * @param file 导入文件
     */
    @SaCheckPermission("canshu:index:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<CanshuDTO> excelResult = ExcelUtil.importExcel(file.getInputStream(), CanshuDTO.class, true);
        List<CanshuDTO> volist = excelResult.getList();
        List<Canshu> list = BeanUtil.copyToList(volist, Canshu.class);
        service.saveBatch(list);
        return R.ok(excelResult.getAnalysis());
    }

    /**
     * 导出excel 表格
     * @param param 导出条件
     */
    @GetMapping("/export")
    public void export(@Validated CanshuDTO param, HttpServletResponse response) {
        List<Canshu> list = service.queryList(param);
        ExcelUtil.exportExcel(list, "订单信息", Canshu.class, response);
    }
}
